﻿$(document).ready(function () {
    $("#find_by_role").on("change", function () {

        var id = document.getElementById("find_by_role").value;
        if (id == 0)
            document.getElementById("btn_role_edit").style.display = "none";
        else {
            document.getElementById("btn_role_edit").style.display = "inline-block";

        }
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                document.getElementById("sort_staff").innerHTML = xhttp.responseText;
                $(".on1").click(function () {
                    if ($(this).hasClass("off")) {
                        $(this).removeClass("off");
                        $(this).removeClass("fa-rotate-180");
                        $(this).removeClass("w3-text-red");
                        $(this).addClass("w3-text-teal");
                    }
                    else {
                        $(this).addClass("w3-text-red");
                        $(this).removeClass("w3-text-teal");
                        $(this).addClass("fa-rotate-180");
                        $(this).addClass("off");
                    }

                    var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 || xhttp.status == 200) {

                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                    xhttp.send();
                });
                $(".change_role").on("change", function () {

                    document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    document.getElementById("confirm_role").innerHTML = this.value;

                    document.getElementById("role").style.display = "block";
                });
                $(".del_staff").on("click", function () {
                    document.getElementById("del_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    document.getElementById('xoanhanvien').style.display = 'block';
                });
                $("#staff_del_yes").on("click", function () {
                    var id = document.getElementById("del_id").innerHTML;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                            document.getElementById('xoanhanvien').style.display = 'none';
                            $("#staff" + id).remove();
                            $(".on1").click(function () {
                                if ($(this).hasClass("off")) {
                                    $(this).removeClass("off");
                                    $(this).removeClass("fa-rotate-180");
                                    $(this).removeClass("w3-text-red");
                                    $(this).addClass("w3-text-teal");
                                }
                                else {
                                    $(this).addClass("w3-text-red");
                                    $(this).removeClass("w3-text-teal");
                                    $(this).addClass("fa-rotate-180");
                                    $(this).addClass("off");
                                }

                                var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                                    }
                                };
                                xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                                xhttp.send();
                            });
                            $(".change_role").on("change", function () {

                                document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                document.getElementById("confirm_role").innerHTML = this.value;

                                document.getElementById("role").style.display = "block";
                            });
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/del_staff.aspx?id=" + id, true);
                    xhttp.send();
                });
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/find_by_role.aspx?id=" + id, true);
        xhttp.send();
    });

    $("#find_by_name").on("keyup", function () {
        var name = document.getElementById("find_by_name").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                document.getElementById("sort_staff").innerHTML = xhttp.responseText;
                $(".on1").click(function () {
                    if ($(this).hasClass("off")) {
                        $(this).removeClass("off");
                        $(this).removeClass("fa-rotate-180");
                        $(this).removeClass("w3-text-red");
                        $(this).addClass("w3-text-teal");
                    }
                    else {
                        $(this).addClass("w3-text-red");
                        $(this).removeClass("w3-text-teal");
                        $(this).addClass("fa-rotate-180");
                        $(this).addClass("off");
                    }

                    var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                    xhttp.send();
                });
                $(".change_role").on("change", function () {

                    document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    document.getElementById("confirm_role").innerHTML = this.value;

                    document.getElementById("role").style.display = "block";
                });
                $(".del_staff").on("click", function () {
                    document.getElementById("del_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    document.getElementById('xoanhanvien').style.display = 'block';
                });
                $("#staff_del_yes").on("click", function () {
                    var id = document.getElementById("del_id").innerHTML;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                            document.getElementById('xoanhanvien').style.display = 'none';
                            $("#staff" + id).remove();
                            $(".on1").click(function () {
                                if ($(this).hasClass("off")) {
                                    $(this).removeClass("off");
                                    $(this).removeClass("fa-rotate-180");
                                    $(this).removeClass("w3-text-red");
                                    $(this).addClass("w3-text-teal");
                                }
                                else {
                                    $(this).addClass("w3-text-red");
                                    $(this).removeClass("w3-text-teal");
                                    $(this).addClass("fa-rotate-180");
                                    $(this).addClass("off");
                                }

                                var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                                    }
                                };
                                xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                                xhttp.send();
                            });
                            $(".change_role").on("change", function () {

                                document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                document.getElementById("confirm_role").innerHTML = this.value;

                                document.getElementById("role").style.display = "block";
                            });
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/del_staff.aspx?id=" + id, true);
                    xhttp.send();
                });
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/find_by_name.aspx?name=" + name, true);
        xhttp.send();
    });

    $(".on1").click(function () {
        if ($(this).hasClass("off")) {
            $(this).removeClass("off");
            $(this).removeClass("fa-rotate-180");
            $(this).removeClass("w3-text-red");
            $(this).addClass("w3-text-teal");
        }
        else {
            $(this).addClass("w3-text-red");
            $(this).removeClass("w3-text-teal");
            $(this).addClass("fa-rotate-180");
            $(this).addClass("off");
        }

        var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
        xhttp.send();
    });

    $(".change_role").on("change", function () {
        document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
        document.getElementById("confirm_role").innerHTML = this.value;

        document.getElementById("role").style.display = "block";
    });

    $("#role_yes").on("click", function () {
        var id = document.getElementById("confirm_id").innerHTML;
        var role = document.getElementById("confirm_role").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                document.getElementById("role").style.display = 'none';
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/change_role.aspx?id=" + id + "&role=" + role, true);
        xhttp.send();
    });

    $("#btn_role_edit").on("click", function () {
        document.getElementById("txt_role_edit").value = $("#find_by_role").find(":selected").text();
    });

    $("#btn_role_edited").on("click", function () {
        var id = document.getElementById("find_by_role").value;
        var name = document.getElementById("txt_role_edit").value;
        //alert(id + " " + name);

        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/edit_role.aspx?id=" + id + "&name=" + name, true);
        xhttp.send();
    });
    $("#btn_role_del").on("click", function () {
        var count = $(".staff").length;
        if (count != 0) {
            alert("Không thể xóa quyền này khi vẫn còn nhân viên");
        } else {
            var id = document.getElementById("find_by_role").value;
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 || xhttp.status == 200) {
                    location.reload(true);
                }
            };
            xhttp.open("GET", "../ajax/asp/staff/del_role.aspx?id=" + id, true);
            xhttp.send();
        }
    });
    $("#btn_role_add").on("click", function () {
        var name = document.getElementById("txt_role_add").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                location.reload(true);
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/add_role.aspx?&name=" + name, true);
        xhttp.send();
    });

    $("#btn_sign_in").on("click", function () {
        if (document.getElementById("find_by_role").value == 0) {
            alert("Bạn chưa chọn quyền");
        }
        else {
            document.getElementById('sign-in-modal').style.display = 'block';
        }
    });

    $("#month").on("change", function () {
        var day = document.getElementById("day").value;
        var month = document.getElementById("month").value;
        var year = document.getElementById("year").value;
        if (month == 2) {
            if (day > 29) {
                if (year % 4 == 0)
                    document.getElementById("day").value = 29;
                else
                    document.getElementById("day").value = 28;
            }
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31)
                    document.getElementById("day").value = 30;
            }
        }
    });

    $("#day").on("change", function () {
        var day = document.getElementById("day").value;
        var month = document.getElementById("month").value;
        var year = document.getElementById("year").value;

        if (month == 2 && day > 28) {
            if (year % 4 == 0)
                document.getElementById("day").value = 29;
            else
                document.getElementById("day").value = 28;
        }
        else {
            if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day == 31) {
                    document.getElementById("day").value = 30;
                }
            }
        }
    });

    $("#year").on("change", function () {
        var day = document.getElementById("day").value;
        var month = document.getElementById("month").value;
        var year = document.getElementById("year").value;

        if (year % 4 == 0) {
            if (month == 2 && day > 29)
                document.getElementById("day").value = 29;
        }
        else {
            if (month == 2 && day > 28)
                document.getElementById("day").value = 28;
        }
    });
    $("#txt_username").on("keyup", function () {
        var username = document.getElementById("txt_username").value;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("error").innerHTML = xhttp.responseText;
                var error = document.getElementById("error").innerHTML;
                if (error.trim() != "0")
                    document.getElementById("error_msg").innerHTML = "Tài khoản đã tồn tại!";
                else
                    document.getElementById("error_msg").innerHTML = "";
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/check_useradmin.aspx?username="+username.trim(), true);
        xhttp.send();
    });
    $("#btn_add_staff").on("click", function () {
        var name = document.getElementById("txt_name").value;
        var username = document.getElementById("txt_username").value;
        var pass = document.getElementById("txt_pass").value;
        var repass = document.getElementById("txt_repass").value;
        var email = document.getElementById("txt_email").value;
        var phone = document.getElementById("txt_phone").value;
        var day = document.getElementById("day").value;
        var month = document.getElementById("month").value;
        var year = document.getElementById("year").value;
        var role = document.getElementById("find_by_role").value;
        var error = document.getElementById("error").innerHTML;
        var sex = "";
        if ($('#male').is(":checked"))
            sex = document.getElementById("male").value;
        if ($('#female').is(":checked"))
            sex = document.getElementById("female").value;

        if (name == "" || name.length < 5)
            alert("Họ và tên quá ngắn");
        else {
            if (username == "" || username.length < 3 || username.length > 30)
                alert("Tên đăng nhập phải từ 3 - 30 ký tự");
            else {
                if (pass == "" || pass.length < 6 || pass.length > 20)
                    alert("Password phải từ 6 - 20 ký tự");
                else {
                    if (repass == "" || repass != pass)
                        alert("Nhập lại password không chính xác");
                    else {
                        if (email == "")
                            alert("Email không được để trống");
                        else {
                            var check = /@/;
                            if (check.test(email) == false) {
                                alert("định dạng email không chính xác")
                            }
                            else {
                                var temp = email.split('@');
                                if (temp.length > 2) {
                                    alert("định dạng email không chính xác");
                                }
                                else {
                                    if (temp[1].toLowerCase() != "gmail.com" && temp[1].toLowerCase() != "yahoo.com" && temp[1].toLowerCase() != "yahoo.com.vn" && temp[1].toLowerCase() != "aol.com" && temp[1].toLowerCase() != "outlook.com" && temp[1].toLowerCase() != "senquocte.com") {
                                        alert("email @" + temp[1] + " không tồn tại hoặc không hỗ trợ");
                                    }
                                    else {
                                        if (sex == "")
                                            alert("Bạn chưa chọn giới tính");
                                        else {
                                            if (day == 0)
                                                alert("bạn chưa chọn ngày sinh");
                                            else {
                                                if (month == 0)
                                                    alert("bạn chưa chọn tháng sinh");
                                                else {
                                                    if (year == 0)
                                                        alert("bạn chưa chọn năm sinh");
                                                    else {
                                                        if(error!=1)
                                                        {
                                                            var xhttp;
                                                        xhttp = new XMLHttpRequest();
                                                        xhttp.onreadystatechange = function () {
                                                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                                                document.getElementById("sign-in-modal").style.display = 'none';
                                                                document.getElementById("sort_staff").innerHTML += xhttp.responseText;
                                                                $(".on1").click(function () {
                                                                    if ($(this).hasClass("off")) {
                                                                        $(this).removeClass("off");
                                                                        $(this).removeClass("fa-rotate-180");
                                                                        $(this).removeClass("w3-text-red");
                                                                        $(this).addClass("w3-text-teal");
                                                                    }
                                                                    else {
                                                                        $(this).addClass("w3-text-red");
                                                                        $(this).removeClass("w3-text-teal");
                                                                        $(this).addClass("fa-rotate-180");
                                                                        $(this).addClass("off");
                                                                    }

                                                                    var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                                                    var xhttp;
                                                                    xhttp = new XMLHttpRequest();
                                                                    xhttp.onreadystatechange = function () {
                                                                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                                                                        }
                                                                    };
                                                                    xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                                                                    xhttp.send();
                                                                });
                                                                $(".change_role").on("change", function () {

                                                                    document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                                                    document.getElementById("confirm_role").innerHTML = this.value;

                                                                    document.getElementById("role").style.display = "block";
                                                                });
                                                                $("#staff_del_yes").on("click", function () {
                                                                    var id = document.getElementById("del_id").innerHTML;
                                                                    var xhttp;
                                                                    xhttp = new XMLHttpRequest();
                                                                    xhttp.onreadystatechange = function () {
                                                                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                                                                            document.getElementById('xoanhanvien').style.display = 'none';
                                                                            $("#staff" + id).remove();
                                                                            $(".on1").click(function () {
                                                                                if ($(this).hasClass("off")) {
                                                                                    $(this).removeClass("off");
                                                                                    $(this).removeClass("fa-rotate-180");
                                                                                    $(this).removeClass("w3-text-red");
                                                                                    $(this).addClass("w3-text-teal");
                                                                                }
                                                                                else {
                                                                                    $(this).addClass("w3-text-red");
                                                                                    $(this).removeClass("w3-text-teal");
                                                                                    $(this).addClass("fa-rotate-180");
                                                                                    $(this).addClass("off");
                                                                                }

                                                                                var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                                                                var xhttp;
                                                                                xhttp = new XMLHttpRequest();
                                                                                xhttp.onreadystatechange = function () {
                                                                                    if (xhttp.readyState == 4 || xhttp.status == 200) {
                                                                                    }
                                                                                };
                                                                                xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                                                                                xhttp.send();
                                                                            });
                                                                            $(".change_role").on("change", function () {

                                                                                document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                                                                document.getElementById("confirm_role").innerHTML = this.value;

                                                                                document.getElementById("role").style.display = "block";
                                                                            });
                                                                        }
                                                                    };
                                                                    xhttp.open("GET", "../ajax/asp/staff/del_staff.aspx?id=" + id, true);
                                                                    xhttp.send();
                                                                });
                                                                $(".del_staff").on("click", function () {
                                                                    document.getElementById("del_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                                                                    document.getElementById('xoanhanvien').style.display = 'block';
                                                                });
                                                            }
                                                        };
                                                        xhttp.open("GET", "../ajax/asp/staff/add_staff.aspx?name=" + name + "&username=" + username + "&pass=" + pass + "&repass=" + repass + "&email=" + email + "&phone=" + phone + "&day=" + day + "&month=" + month + "&year=" + year + "&sex=" + sex + "&role=" + role, true);
                                                        xhttp.send();
                                                        }
                                                        
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    $(".del_staff").on("click", function () {
        document.getElementById("del_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
        document.getElementById('xoanhanvien').style.display = 'block';
    });
    $("#staff_del_yes").on("click", function () {
        var id = document.getElementById("del_id").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 || xhttp.status == 200) {
                document.getElementById('xoanhanvien').style.display = 'none';
                $("#staff" + id).remove();
                $(".on1").click(function () {
                    if ($(this).hasClass("off")) {
                        $(this).removeClass("off");
                        $(this).removeClass("fa-rotate-180");
                        $(this).removeClass("w3-text-red");
                        $(this).addClass("w3-text-teal");
                    }
                    else {
                        $(this).addClass("w3-text-red");
                        $(this).removeClass("w3-text-teal");
                        $(this).addClass("fa-rotate-180");
                        $(this).addClass("off");
                    }

                    var id = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    var xhttp;
                    xhttp = new XMLHttpRequest();
                    xhttp.onreadystatechange = function () {
                        if (xhttp.readyState == 4 || xhttp.status == 200) {
                        }
                    };
                    xhttp.open("GET", "../ajax/asp/staff/set_active.aspx?id=" + id, true);
                    xhttp.send();
                });
                $(".change_role").on("change", function () {

                    document.getElementById("confirm_id").innerHTML = this.parentElement.parentElement.getElementsByClassName("staff_id")[0].innerHTML;
                    document.getElementById("confirm_role").innerHTML = this.value;

                    document.getElementById("role").style.display = "block";
                });
            }
        };
        xhttp.open("GET", "../ajax/asp/staff/del_staff.aspx?id=" + id, true);
        xhttp.send();
    });
});